<template>
  <div class="app-container">
    <el-container>
      <el-container>
        <el-header>
          <p>基本信息</p>
        </el-header>
        <el-main>
          <el-form class="form-container" :model="postForm" :rules="rules" ref="postForm">
            <el-row>
              <el-col :span="12">
                <el-tooltip class="item" effect="dark" content="商品名称" placement="top">
                  <el-form-item label-width="50px" label="来源:" class="postInfo-container-item">
                    <el-input placeholder="商品名称" style='min-width:150px;' v-model="postForm.source_name">
                    </el-input>
                  </el-form-item>
                </el-tooltip>
              </el-col>
              <el-col :span="8">
                <el-tooltip class="item" effect="dark" content="商品款号" placement="top">
                  <el-form-item label-width="50px" label="来源:" class="postInfo-container-item">
                    <el-input placeholder="商品款号" style='min-width:150px;' v-model="postForm.source_name">
                    </el-input>
                  </el-form-item>
                </el-tooltip>
              </el-col>
            </el-row>
            <el-form-item style="margin-bottom: 40px;" label-width="45px" label="摘要:">
              <el-input type="textarea" class="article-textarea" :rows="1" autosize placeholder="请输入内容" v-model="postForm.content_short">
              </el-input>
              <span class="word-counter" >{{'2'}}字</span>
            </el-form-item>
          </el-form>
        </el-main>
      </el-container>
      <el-aside>
        <div class="singleImageUpload2 upload-container">
          <el-upload class="image-uploader" :data="dataObj" drag :multiple="false" :show-file-list="false" action="https://httpbin.org/post"
            :on-success="handleImageScucess">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">Drag或<em>点击上传</em></div>
          </el-upload>
          <div v-show="imageUrl.length>0" class="image-preview">
            <div class="image-preview-wrapper" v-show="imageUrl.length>1">
              <img :src="imageUrl">
              <div class="image-preview-action">
                <i @click="rmImage" class="el-icon-delete"></i>
              </div>
            </div>
          </div>
        </div>
      </el-aside>
    </el-container>
    <div class="editor-container">
      <tinymce :height=400 ref="editor" v-model="postForm.content"></tinymce>
    </div>
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import { validateURL } from '@/utils/validate'
export default {
  watch: {
  },
  computed: {
    imageUrl() {
      return this.value
    }
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        this.$message({
          message: rule.field + '为必传项',
          type: 'error'
        })
        callback(null)
      } else {
        callback()
      }
    }
    const validateSourceUri = (rule, value, callback) => {
      if (value) {
        if (validateURL(value)) {
          callback()
        } else {
          this.$message({
            message: '外链url填写不正确',
            type: 'error'
          })
          callback(null)
        }
      } else {
        callback()
      }
    }
    return {
      tempUrl: '',
      dataObj: { token: '', key: '' },
      value: [],
      postForm: {
        content: ''
      },
      rules: {
        image_uri: [{ validator: validateRequire }],
        title: [{ validator: validateRequire }],
        content: [{ validator: validateRequire }],
        source_uri: [{ validator: validateSourceUri, trigger: 'blur' }]
      }
    }
  },
  methods: {
    rmImage() {
      this.value = ''
      // this.emitInput('')
    },
    emitInput(val) {
      this.$emit('input', val)
    },
    handleImageScucess(file) {
      console.log(file)
      this.value = file.files.file
      // this.emitInput(this.tempUrl)
    }
  },
  components: {
    Tinymce
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.el-header, .el-footer {
    // background-color: #B3C0D1;
    color: #333;
    text-align: center;
    line-height: 60px;
  }

  .el-aside {
    // background-color: #D3DCE6;
    width: 200px;
    color: #333;
    text-align: center;
    line-height: 200px;
  }
  .image-preview {
    width: 200px;
    height: 200px;
    position: relative;
    border: 1px dashed #d9d9d9;
    float: left;
    margin-left: 50px;
    .image-preview-wrapper {
      position: relative;
      width: 100%;
      height: 100%;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .image-preview-action {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      cursor: default;
      text-align: center;
      color: #fff;
      opacity: 0;
      font-size: 20px;
      background-color: rgba(0, 0, 0, .5);
      transition: opacity .3s;
      cursor: pointer;
      text-align: center;
      line-height: 200px;
      .el-icon-delete {
        font-size: 36px;
      }
    }
    &:hover {
      .image-preview-action {
        opacity: 1;
      }
    }
  }
  .image-app-preview {
    width: 320px;
    height: 180px;
    position: relative;
    border: 1px dashed #d9d9d9;
    float: left;
    margin-left: 50px;
    .app-fake-conver {
      height: 44px;
      position: absolute;
      width: 100%; // background: rgba(0, 0, 0, .1);
      text-align: center;
      line-height: 64px;
      color: #fff;
    }
  }
</style>


